#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(){
    int fd[2];
    int ret = pipe(fd);
    if(ret < 0){
        perror("pipe");
        return 0;
    }

    printf("fd[0]:%d\n", fd[0]);
    printf("fd[1]:%d\n", fd[1]);

    pid_t pid = fork();
    if(pid < 0){
        perror("fork");
        return 0;
    }else if(pid == 0){
        //child-wirte
        close(fd[0]);
        const char* str = "abc";
        write(fd[1], str, strlen(str)); 
        const char* str1 = "def";
        write(fd[1], str1, strlen(str1)); 
    }else{
        //father-read
        sleep(1);
        close(fd[1]);
        char buf[1024] = {0};
        read(fd[0], buf, sizeof(buf) - 1);
        printf("i am father: read buf: [%s]\n", buf);
    }

    while(1){
        sleep(1);
    }
return 0;
}
